﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Resources;
using Smatime.Web.DAO;
using Smatime.Web.Models;

namespace Smatime.Web.Controllers
{
    public class CompanyUserController : ApiController
    {
        // GET api/companyuser
        [Route("api/companyuser/user/{CompanyId}")]
        public IEnumerable<CompanyUser> GetUsers(int CompanyId)
        {
            return new CompanyUserRepository().GetUserByCompanyId(CompanyId);
        }

        // GET api/companyuser/5
        public CompanyUser Get(int id)
        {
            return new CompanyUserRepository().GetById(id);
        }

        // POST api/companyuser
        public HttpResponseMessage Post([FromBody] CompanyUser User)
        {

            try
            {
                var userAccesssRepos = new UserAccessRepository();

                var userEmail = userAccesssRepos.GetByEmail(User.Email);
                if (userEmail.IsNull())
                {
                    // Insert user access 
                    var userAccess = new UserAccess
                    {
                        Email = User.Email,
                        Password = User.Password
                    };
                    userAccesssRepos.Insert(userAccess);
                    // Insert CompanyUserRepository
                    var newUserAccess = userAccesssRepos.GetByEmail(User.Email);
                    if (newUserAccess.IsNull())
                    {
                        var createfail = "Create new user access fail !!";
                        Request.CreateResponse(HttpStatusCode.BadRequest, createfail);
                    }
                    else {

                        var CompanyUserRepos = new CompanyUserRepository();
                        var companyUser = new CompanyUser
                        {
                            CompanyId = User.CompanyId,
                            UserAccessId = newUserAccess.Id,
                            Name = User.Name,
                            RowStatus = 1
                        };
                        CompanyUserRepos.Insert(companyUser);

                        var companyUserIdRepos = new CompanyUserInformationRepository();
                        var companyUserId = companyUserIdRepos.GetByEmail(User.Email);
                        // insert store for company user
                        var userStoreRes = new UserStroreMainRepository();
                        foreach (var storeId in User.StoreIds)
                        {
                            var userStore = new UserStore
                            {
                                CompanyUserId = companyUserId.CompanyUserId,
                                CompanyStoreId = storeId
                            };
                            userStoreRes.Insert(userStore);
                        }
                        return Request.CreateResponse(HttpStatusCode.OK);
                    }
                }
                var mes = "Email already exist !!";
                return Request.CreateResponse(HttpStatusCode.BadRequest, mes);
            }
            catch (Exception ex)
            {

                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }
        }

        // PUT api/companyuser/5
        public HttpResponseMessage Put(int id, [FromBody] CompanyUser user)
        {
            try
            {
                var userAccesssRepos = new UserAccessRepository();
                    // update Company User properties
                    var repos = new CompanyUserRepository();
                    user.Id = id;
                    repos.Update(user);

                    // update User Access properties
                    var userAccess = new UserAccess
                    {
                        Id = user.UserAccessId,
                        Email = user.Email,
                        Password = user.Password
                    };
                    userAccesssRepos.Update(userAccess);

                    //Update to Store list of the companyStaff
                    var userStore = new UserStroreMainRepository();
                    userStore.UpdateStoreListByUser(user.Id, user.StoreIds);

                    return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }
          
        }

        // DELETE api/companyuser/5
        public void Delete(int id)
        {
        }
    }
}